import BaseCURDV2Api from '@/framework/infra/base/BaseCURDV2Api'
import ApiAdaptor from '@/framework/infra/base/ApiAdaptor'
import CollectionUtil from '@/framework/infra/collection/CollectionUtil'

class NewProductRegisterApi extends BaseCURDV2Api {
  constructor() {
    super('pdt/new-product-register')
  }

  chosen(data) {
    return this.post('chosen', data)
  }

  list(params) {
    const getParams = { ...params }
    Object.keys(getParams).forEach(key => {
      if (Array.isArray(getParams[key])) {
        getParams[key] = getParams[key].join(',')
      }
    })
    return this.get('list-page', getParams).then(data => {
      data.data = data.data.map(row => {
        // row.productCode = row.productCode || row.productCode
        // row.onPrice = row.price
        // onPrice = row.price 替换row.price 里的数字为它的/0.7
        row.onePrice = row.price ? row.price.replace(/(\d+)/g, function(s, s1, s2) {
          return Math.round(s / 0.8)
        }) : ''
        return row
      })
      return data
    })
  }

  /**
   * 美工已处理
   */
  artHandled(id) {
    return this.post('art-handled', { id })
  }

  cancelArtHandled(id) {
    return this.post('cancel-art-handled', { id })
  }

  updateTitle(data) {
    return this.post('update-title', data)
  }

  modifyTitle(data) {
    return this.post('modify-title', data)
  }

  lastProductCode(storeId) {
    return this.post('last-product-code', { storeId })
  }

  queryStyleSelectorStatistics({
    dateRange: [startDate, endDate],
    storeId,
    styleSelectorId: userId
  }) {
    return this.get('style-selector-statistics', { startDate, endDate, storeId, userId }).then(data => {
      return ApiAdaptor.rowsAdaptor(data, row => {
        row.countPercent = ((row.totalCount > 0 ? row.count / row.totalCount : 0) * 100).toFixed(2)
        return row
      })
    })
  }

  queryArtDesignerStatistics({
    dateRange: [startDate, endDate],
    storeId,
    artDesignerId: userId
  }) {
    return this.get('art-designer-statistics', { startDate, endDate, storeId, userId })
  }

  queryStyleStatistics({
    dateRange: [startDate, endDate],
    storeId
  }) {
    return this.get('style-statistics', { startDate, endDate, storeId })
  }

  queryOpAssistantIdStatistics({
    dateRange: [startDate, endDate],
    storeId,
    opAssistantId: userId
  }) {
    return this.get('op-assistant-statistics', { startDate, endDate, storeId, userId })
  }

  listByTbLinkUk(tbLink) {
    return this.get('list-by-tb-link-uk', { tbLink })
  }

  queryChannelStatistics({
    dateRange: [startDate, endDate],
    storeId,
    userId
  }) {
    return this.get('channel-statistics', { startDate, endDate, storeId, userId })
  }

  queryCategoriesStatistics({
    dateRange: [startDate, endDate],
    storeId,
    categories
  }) {
    return this.get('categories-statistics', { startDate, endDate, categories, storeId })
  }

  queryArtDesignerWorkCountStatics(startDate, endDate) {
    return this.get('designer-statistics', { startDate, endDate })
  }

  queryArtDesignerDailyWorkCountStatics(startDate, endDate) {
    return this.get('daily-designer-statistics', { startDate, endDate })
  }

  queryStoreStatistics(startDate, endDate, styleList = [], channelList = [], categoriesList = []) {
    return this.get('store-statistics', { startDate, endDate }).then(data => {
      const rows = ApiAdaptor.rowsAdaptor(data, row => {
        if (row.styleCountData) {
          row.styleCountDataMap = row.styleCountData.reduce((r, a) => {
            r[a.title] = a.count
            return r
          }, {})
        } else {
          row.styleCountDataMap = {}
        }
        if (row.channelCountData) {
          row.channelCountData = row.channelCountData.reduce((r, a) => {
            r[a.title] = a.count
            return r
          }, {})
        } else {
          row.channelCountData = {}
        }
        if (row.categoryCountData) {
          row.categoryCountDataMap = row.categoryCountData.reduce((r, a) => {
            r[a.title] = a.count
            return r
          }, {})
        } else {
          row.categoryCountDataMap = {}
        }
        return row
      })
      if (rows.length > 1) {
        const summary = {
          title: '汇总',
          totalCount: CollectionUtil.sum(rows, el => el.totalCount),
          count: CollectionUtil.sum(rows, el => el.count),
          channelCountData: channelList.reduce((r, a) => {
            r[a.value] = CollectionUtil.sum(rows, row => parseInt(row.channelCountData[a.value] || 0))
            return r
          }, {}),
          lastSameCount: CollectionUtil.sum(rows, el => el.lastSameCount),
          lastCount: CollectionUtil.sum(rows, el => el.lastCount),
          bloggerNum: CollectionUtil.sum(rows, el => el.bloggerNum),
          lastBloggerNum: CollectionUtil.sum(rows, el => el.lastBloggerNum),
          bloggerStyleNum: CollectionUtil.sum(rows, el => el.bloggerStyleNum),
          sameCount: CollectionUtil.sum(rows, el => el.sameCount),
          styleCountDataMap: styleList.reduce((r, a) => {
            r[a.value] = CollectionUtil.sum(rows, row => parseInt(row.styleCountDataMap[a.value] || 0))
            return r
          }, {}),
          categoryCountDataMap: categoriesList.reduce((r, a) => {
            r[a.value] = CollectionUtil.sum(rows, row => parseInt(row.categoryCountDataMap[a.value] || 0))
            return r
          }, {})
        }
        // console.log(summary)
        summary.countRate = summary.totalCount > 0 ? (summary.count / summary.totalCount * 100).toFixed(2) : 100
        summary.sameCountRate = summary.count > 0 ? (summary.sameCount / summary.count * 100).toFixed(2) : 100
        // summary.bloggerNumRate = summary.count > 0 ? (summary.bloggerNum / summary.count * 100).toFixed(2) : 100
        summary.bloggerStyleRate = summary.count > 0 ? (summary.bloggerStyleNum / summary.count * 100).toFixed(2) : 100
        summary.countIncRate = summary.lastCount > 0 ? ((summary.count - summary.lastCount) / summary.lastCount).toFixed(2) : 100
        summary.bloggerIncRate = summary.lastBloggerNum > 0 ? ((summary.bloggerNum - summary.lastBloggerNum) / summary.lastBloggerNum).toFixed(2) : 100

        rows.push(summary)
      }
      return rows
    })
  }

  findFirstByProductNo(productNo) {
    return this.get('find-by-product-no', { productNo })
  }

  artVideoHandled(id) {
    return this.post('video-pick', { id })
  }

  batchArtVideoHandled(idList) {
    return this.post('batch-video-pick', { idList })
  }

  cancelArtVideoHandled(id) {
    return this.post('cancel-video-pick', { id })
  }
}

export const newProductRegisterApi = new NewProductRegisterApi()
